<template>
  <div>
    <BasicTable
      @register="registerTable"
      @resize-column="handleResizeColumn"
      :canResize="false"
      :scroll="{ x: 'max-content' }"
    >
      <template #toolbar>
        <a-button type="primary" @click="handleExport">导出</a-button>
      </template>
    </BasicTable>
    <CarModal1 @register="registerCar" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
  name: 'CarTeam',
});
</script>
<script lang="ts" setup>
import { onMounted, ref } from 'vue';
import { BasicTable, useTable } from '@/components/Table';
import { useModal } from '@/components/Modal';
import CarModal1 from './components/CarModal.vue';
import { carColumns, carSearchFormSchema } from './data';
import { useMessage } from '@/hooks/web/useMessage';
import { defHttp } from '@/utils/http/axios';
import { useRoute, useRouter } from 'vue-router';
import { downloadBlob } from '@/utils/file/download';
const { createMessage } = useMessage();
const router = useRoute();
const [registerCar, { openModal: openCarModal }] = useModal();
const [registerTable, { reload, getForm, getSelectRows, getSelectRowKeys }] =
  useTable({
    //title: '列表',
    api: '/coupon/coupon/list',
    columns: carColumns,
    showIndexColumn: true,
    useSearchForm: true,
    indexColumnProps: {
      fixed: 'left',
    },
    beforeFetch: (info) => {
      if (router.query.companyId) {
        return { ...info, companyId: router.query.companyId };
      }
      return;
    },
    formConfig: {
      labelWidth: 110,
      hideLabel: true,
      schemas: carSearchFormSchema,
      baseColProps: { span: 6 },
    },
    pagination: true,
    bordered: true,
    rowKey: 'id',
    rowSelection: {
      type: 'checkbox',
    },
  });
const exportLoading = ref(false);
onMounted(() => {});
function handleCreate() {
  openCarModal(true, {
    isUpdate: false,
    companyId: router.query.companyId,
  });
}
function handleEdit() {
  if (getSelectRows().length === 0 || getSelectRows().length > 1) {
    createMessage.error('请选择一个车辆进行编辑');
  } else {
    openCarModal(true, {
      isUpdate: true,
      id: getSelectRows()[0].id,
    });
  }
}
async function handleDelete() {
  if (getSelectRows().length === 0) {
    createMessage.error('请选择一个车辆');
  } else {
    const res = await defHttp.post({
      url: `/admin/car/delete`,
      data: getSelectRowKeys(),
    });
    createMessage.success("已删除");
    reload();
  }
}
const handleExport = async () => {
  try {
    exportLoading.value = true;
    let param;
    if (getSelectRows().length > 0) {
      const ids = [];
      for (let a = 0; a < getSelectRows().length; a++) {
        ids.push(getSelectRows()[a].id);
      }
      debugger
      param = {
        ids: ids,
        importType: 'checkBox',
      };
    } else {
      param = {
        ...getForm().getFieldsValue(),
      };
    }
    const res = await defHttp.post(
      {
        url: '/coupon/coupon/couponExcell',
        responseType: 'blob',
        data: JSON.stringify(param),
      },
      { isReturnNativeResponse: true },
    );
    downloadBlob(res);
  } catch (error) {
    console.log(error);
  } finally {
    exportLoading.value = false;
  }
};
function handleSuccess() {
  reload();
}
</script>
